package com.ruoyi.teaching.listener;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.teaching.domain.YaHighInterview;
import com.ruoyi.teaching.dto.CanalHighInterviewDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.baomidou.mybatisplus.core.assist.ISqlRunner.INSERT;

/**
 * @author 15332
 * @version 1.0
 * @date 2025/9/12 17:16
 * @description TODO
 */
@Slf4j
@Component
public class SpringRabbitListener {

  @Autowired
  private ElasticsearchTemplate elasticsearchTemplate;

    /*@RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "teaching.interview.ex.addOrupdate"),
            exchange = @Exchange(name = "teaching.interview.ex", type = ExchangeTypes.DIRECT),
            key = {"teaching.interview.ex.add", "teaching.interview.ex.update"}
    ))
    public void listenDirectQueue1(YaHighInterview yaHighInterview){
        System.out.println("大量数据同步到ES:"+yaHighInterview);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "teaching.interview.ex.del"),
            exchange = @Exchange(name = "teaching.interview.ex", type = ExchangeTypes.DIRECT),
            key = {"teaching.interview.ex.update.delete"}
    ))
    public void listenDirectQueue2(Long id){
        System.out.println("大量数据从ES删除:"+id);
    }*/


  @RabbitListener(bindings = @QueueBinding(
    value = @Queue(name = "canal.queue"),
    exchange = @Exchange(name = "canal.exchange.data",type = ExchangeTypes.FANOUT)
  ))
  public void listenDirectQueue3(String json) {
    // 添加输入验证和日志
    if (StringUtils.isEmpty(json)) {
      log.warn("接收到空的JSON消息");
      return;
    }

    log.info("接收到的消息内容: {}", json);
    ObjectMapper objectMapper = new ObjectMapper();
    try {
      CanalHighInterviewDTO canalHighInterviewDTO = objectMapper.readValue(json, CanalHighInterviewDTO.class);
      String type = canalHighInterviewDTO.getType();
      if (StringUtils.isEmpty(type)) {
        return;
      }
//    更新操作
      switch (type) {
        case "INSERT":
          log.info("新增数据:" + canalHighInterviewDTO);
          elasticsearchTemplate.save(canalHighInterviewDTO);
          break;
        case "UPDATE":
          log.info("更新数据:" + canalHighInterviewDTO);
          elasticsearchTemplate.save(canalHighInterviewDTO);
          break;
        case "DELETE":
          log.info("删除数据:" + canalHighInterviewDTO);
          elasticsearchTemplate.delete(canalHighInterviewDTO);
          break;
      }
    } catch (Exception e) {
      log.error("获取数据异常:",e);
      throw new ServiceException("ES同步异常");
    }
  }

}
